using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 束缚Buff - 大幅降低目标移动速度
/// </summary>
public class SnareBuff : BuffBase
{
    private float m_OriginalSpeed;
    private float m_SpeedReduction;

    public SnareBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
        if (buffData is SnareData snareData)
        {
            m_SpeedReduction = snareData.SpeedReduction;
        }
        else
        {
            m_SpeedReduction = 0.8f; // 默认降低80%移动速度
        }
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        // 保存原始速度
        if (Target.MoveComponent != null)
        {
            m_OriginalSpeed = Target.MoveComponent.Speed;
            Target.MoveComponent.SetSpeed(m_OriginalSpeed * (1.0f - m_SpeedReduction));
        }
        
        // 播放束缚特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        // 设置动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("snared", true);
        }
        
        Logger.Log($"{Target.name} 被束缚了，移动速度降低 {m_SpeedReduction * 100}%");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        // 束缚期间不需要特殊处���
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log($"{Target.name} 束缚效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        // 恢复原始速度
        if (Target.MoveComponent != null)
        {
            Target.MoveComponent.SetSpeed(m_OriginalSpeed);
        }
        
        // 恢复动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("snared", false);
        }
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Snare"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return true; // 束缚效果可以叠加，刷新持续时���
    }
}
